<?php  
	/**
	 * 标签设置
	 */
	class HtmlTag{
		//定义标签类型
		public $tag = array(
			//设置block标签为快标签
			'channel' => array('block'=>1),
			'nav'	  => array('block'=>1),
			'arclist' => array('block'=>1),
			'tag'	  => array('block'=>1),
			'flink'	  => array('block'=>1)
		);

		/**
		 * [_flink 友情链接]
		 * @param  [type] $attr    [description]
		 * @param  [type] $content [description]
		 * @param  [type] $parse   [description]
		 * @return [type]          [description]
		 */
		public function _flink($attr,$content,$parse){
			//取多少条
			$row = isset($attr['row']) ? $attr['row'] : NULL;
			//排序
			$order = isset($attr['order']) ? $attr['order'] : 'sort DESC';
			$str = <<<str
<?php \$cate = K('Flink') -> limit({$row}) -> order('{$order}') -> all(); ?>
<foreach from="\$cate" value="\$field">
<?php
\$field['img'] = '__ROOT__' . \$field['logo'];
?>
	{$content}
</foreach>
str;
			return $str;
		}

		/**
		 * [_tag 标签]
		 * @param  [type] $attr    [description]
		 * @param  [type] $content [description]
		 * @param  [type] $parse   [description]
		 * @return [type]          [description]
		 */
		public function _tag($attr,$content,$parse){
			//取多少条
			$row = isset($attr['row']) ? $attr['row'] : 0;
			//分类id
			if(isset($attr['cid'])){
				$cid = $attr['cid'];
			}else if($getCid = Q('get.cid',0,'intval')){
				$cid = $getCid;
			}else{
				$cid = 0;
			}
			//获得tag的tid
			$tid = K('ArticleTag') ->field(array('tag_tid')) -> where(array('category_cid'=>$cid)) -> all();
			$tempAyy = array();
			foreach ($tid as $v) {
				$tempAyy[] = $v['tag_tid'];
			}
			$tempstr = implode(',', $tempAyy);
			$class = isset($attr['class']) ? $attr['class'] : '';

			//\$tag = K('Tag') -> in(array($tempstr)) -> all();
			//定界符组合数据
			$str = <<<str
<?php
	\$tag = K('Tag') -> in(array($tempstr)) -> all();
?>	
<foreach from="\$tag" value="\$field">
<?php
\$field['url'] = U('List/index',array('tid'=>\$field['tid']));

\$field['class'] = \$field['tid'] == Q('get.tid') ? $class : '';
?>
	{$content}
</foreach>		
str;
			return $str;
		}

		/**
		 * [_channel 分类标签]
		 * @param  [type] $attr    [标签属性]
		 * @param  [type] $content [标签内容]
		 * @param  [type] $parse   [解析]
		 * @return [type]          [反出字符串]
		 */
		public function _channel($attr, $content, $parse){
			//分类必须显示
			$where = 'is_show=1';
			//属性类型
			$type = isset($attr['type']) ? $attr['type'] : NULL;
			//几行
			$row = isset($attr['row']) ? $attr['row'] : NULL;
			//排序
			$order = isset($attr['order']) ? $attr['order'] : 'csort DESC';
			$class = isset($attr['class']) ? $attr['class'] : '';

			//分类id
			if(isset($attr['cid'])){
				$cid = $attr['cid'];
			}else if($getCid = Q('get.cid',0,'intval')){
				$cid = $getCid;
			}else{
				$cid = 0;
			}

			if($cid) $where .= ' and cid=' . $cid;

			switch ($type) {
				case 'self':
					$pid = K('Category')->get_field($cid,'pid');
					$where .= ' and pid=' . $pid;
					break;
				
				default:
					# code...
					break;
			}

			$str = <<<str
<?php \$cate = K('Category')->where('{$where}')->limit({$row})->order('{$order}')->all(); ?>
<foreach from="\$cate" value="\$field">
<?php 
\$field['curl'] = get_cate_url(\$field['is_listhtml'],\$field['htmldir'],\$field['cid']);
\$field['class'] = \$field['cid'] == Q('get.cid') ? $class : '';
?>
{$content}
</foreach>
str;
		return $str;

	}

		/**
		 * [_nav 导航标签]
		 * @param  [type] $attr    [标签属性]
		 * @param  [type] $cintent [标签内容]
		 * @param  [type] $parse   [解析]
		 * @return [type]          [反出字符串]
		 */
		public function _nav($attr, $content, $parse){
			//分类必须显示
			$where = 'is_show=1 and is_nav=1';
			//属性cid
			$cid = isset($attr['cid']) ? $attr['cid'] : 0;
			//属性类型
			$type = isset($attr['type']) ? $attr['type'] : NULL;
			//几行
			$row = isset($attr['row']) ? $attr['row'] : NULL;
			//排序
			$order = isset($attr['order']) ? $attr['order'] : 'csort DESC';
			$class = isset($attr['class']) ? $attr['class'] : '';

			switch ($type) {
				case 'self':
					$pid = K('Category')->get_field($cid,'pid');
					$where .= ' and pid=' . $pid;
					break;
				
				default:
					# code...
					break;
			}
			$str = <<<str
<?php \$cate = K('Category')->where('{$where}')->limit({$row})->order('{$order}')->all(); ?>
<foreach from="\$cate" value="\$field">
<?php 
\$field['curl'] = get_cate_url(\$field['is_listhtml'],\$field['htmldir'],\$field['cid']);
\$field['class'] = \$field['cid'] == Q('get.cid') ? $class : '';
?>
{$content}
</foreach>
str;
		return $str;

	}

		public function _arclist($attr, $content, $parse){
		//必须不在回收站
		$where = 'is_recycle=0';
		//所取行数
		$row = isset($attr['row']) ? $attr['row'] : NULL;
		//文章属性
		$attribute = isset($attr['attr']) ? $attr['attr'] : NULL;
		//如果有置顶或者推荐属性
		if($attribute) $where .= ' and attr like "%'.$attribute.'%"';
		//标题长度
		$titlelen = isset($attr['titlelen']) ? $attr['titlelen'] : 0;
		//分类id
		if(isset($attr['cid'])){
			$cid = $attr['cid'];
		}else if($getCid = Q('get.cid',0,'intval')){
			$cid = $getCid;
		}else{
			$cid = 0;
		}
		if($cid) $where .= ' and category_cid=' . $cid;
		

		//处理标签云
		$tid = isset($_GET['tid']) ? $_GET['tid'] : 0;
		//如果有tid
		if($tid){
			$aidArr = K('ArticleTag')->get_data(array('tag_tid'=>$tid),'article_aid');
			$temp = array();
			foreach ($aidArr as $v) {
				$temp[] = $v['article_aid'];
			}

			$where .= ' and aid in (' . implode(',', $temp) . ')';

		}

		

		//分页
		$pagenum = isset($attr['pagenum']) ? $attr['pagenum'] : 0;
		$pagerows = isset($attr['pagerows']) ? $attr['pagerows'] : 10;



		$str = <<<str
<?php 
if($pagenum){
		if(isset(\$_GET['cid'])){
			\$cateInfo = K('Category')->where(array('cid'=>\$_GET['cid']))->field('is_listhtml,htmldir')->find();
			if(\$cateInfo['is_listhtml']){
				//如果是静态则处理静态地址
				Page::\$staticUrl = '__ROOT__/Static/'.\$cateInfo['htmldir'].'/index_{page}.html';
			}
	}
	
	\$arcTotal = K('ArticleCateView')->where('{$where}')->count();
	\$pageObj = new Page(\$arcTotal,$pagerows,$pagenum);
	//如果存在cid则处理静态地址
	\$page = \$pageObj->show();
	\$arc = K('ArticleCateView')->where('{$where}')->limit({$row})->all(\$pageObj->limit());
}else{
	\$arc = K('ArticleCateView')->where('{$where}')->limit({$row})->all();
}

?>
<foreach from="\$arc" value="\$field">
<?php 
	\$field['url'] = get_arc_url(\$field['is_archtml'],\$field['htmldir'],\$field['aid']);
	if($titlelen) \$field['title'] = mb_substr(\$field['title'], 0,$titlelen,'utf-8');
	\$field['thumb'] = '__ROOT__/' . \$field['thumb'];
	\$field['time'] = date('y-m-d',\$field['sendtime']);
	\$field['total'] = K('Comment')->get_total(\$field['aid']);
?>
{$content}
</foreach>

str;
	return $str;

	}
	}
?>